package com.inei.cie.utilitarios;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.inei.cie.R;
import com.inei.cie.dao.CapituloIIIDao;
import com.inei.cie.servicios.ServicioINEI;

public class EnvioCapitulosAdapter extends BaseAdapter {

	Context context;
	List<String> Capitulos;
	String codigoLocal;

	public EnvioCapitulosAdapter(Context context, List<String> Capitulos, String codigoLocal) {
		this.context = context;
		this.Capitulos = Capitulos;
		this.codigoLocal = codigoLocal;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return Capitulos.size();
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return Capitulos.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public View getView(int pos, View item, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final ViewHolderCapitulos holder;
		if (item == null) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			item = inflater.inflate(R.layout.envio_datos_item, null);
			holder = new ViewHolderCapitulos();
			holder.tvCapitulo = (TextView) item.findViewById(R.id.tvCapitulo);
			holder.bEnviar = (Button) item.findViewById(R.id.bEnviar);
			holder.bVolverEnviar = (Button) item
					.findViewById(R.id.bVolverEnviar);
			item.setTag(holder);
		} else {
			holder = (ViewHolderCapitulos) item.getTag();
		}

		holder.tvCapitulo.setText(Capitulos.get(pos));
		holder.bEnviar.setEnabled(true);
		holder.bVolverEnviar.setEnabled(false);

		holder.bEnviar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				/*try {
					Thread.sleep(1000);
				} catch (Exception e) {

				} finally {
					Toast.makeText(context,
							"Los datos se enviaron correctamente.",
							Toast.LENGTH_LONG).show();
				}*/
				/*ServicioINEI envioDatosCap3 = new ServicioINEI();
				String token = ServicioINEI.getInstance().obtenerToken("7", "41146146", "12346");
				envioDatosCap3.envioDatosCapitulo3(codigoLocal, token);
				holder.bEnviar.setEnabled(false);
				holder.bVolverEnviar.setEnabled(true);*/
			}
		});
		holder.bVolverEnviar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CrearDialogo();
			}
		});
		return item;
	}

	public void CrearDialogo() {
		final Dialog dialogo = new Dialog(context);
		dialogo.setTitle("Confirmaci\u00f3n");

		dialogo.setContentView(R.layout.confirmacion_envio);
		final EditText etclave = (EditText) dialogo.findViewById(R.id.etClave);
		Button bAceptar = (Button) dialogo.findViewById(R.id.bAceptar);
		Button bCancelar = (Button) dialogo.findViewById(R.id.bCancelar);
		bCancelar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialogo.cancel();
			}
		});
		bAceptar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (etclave.getText().toString().trim().equals("")) {
					Toast.makeText(context, "Ingrese la clave",
							Toast.LENGTH_SHORT).show();
				} else if (!etclave.getText().toString().trim().equals("1234")) {
					Toast.makeText(context, "Clave incorrecta",
							Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(context, "Clave correcta",
							Toast.LENGTH_SHORT).show();
					try {
						Thread.sleep(1000);
					} catch (Exception e) {

					} finally {
						Toast.makeText(context,
								"Los datos se enviaron correctamente.",
								Toast.LENGTH_LONG).show();
					}
					dialogo.cancel();
				}

			}
		});
		dialogo.show();

	}

}
